package b1.scenes.fight
{
	import b1.controls.basic.BasicMessage;
	import b1.net.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class Bar extends Sprite
	{
		private var topBarImg:Sprite;
		private var buttomBarImg:Sprite;
		private var max:int;
		public var current:int;
		private var tip:BasicMessage;
		private var tipString:String;
		private var texRoundRecColor:uint;
		//进度条的宽和高
		private var barWidth:int;
		private var barHeight:int;

		private var leftFlag:Boolean;
		private var words:String;
		
		public function Bar(buttomImgStr:String, topImgStr:String, max:int, current:int, width:int, height:int, leftFlag:Boolean = true, words:String = null, texRoundRecColor:uint = 0xFFFFFF)
		{
			super();
			//下图片
			var ButtomBarImg:Class = ResourceHelper.getInstance().getClassByName(buttomImgStr);
			buttomBarImg = new ButtomBarImg();
			buttomBarImg.height = height;
			buttomBarImg.width = width;
			addChild(buttomBarImg);
			//上图片
			var TopBarImg:Class = ResourceHelper.getInstance().getClassByName(topImgStr);
			topBarImg = new TopBarImg();
			topBarImg.height = height;
			topBarImg.width = width;
			addChild(topBarImg);
			
			this.max = max;
			this.current = current;
			this.leftFlag = leftFlag;
			this.barHeight = height;
			this.barWidth = width;	
			this.words = words;
			this.texRoundRecColor = texRoundRecColor;
			
			changeBar(0);
		}
		
		public function onMouseOver(event:MouseEvent):void
		{
			if (null == tip)
			{
				if (null != words)
				{
					tipString = words + ": " + this.current.toString() + "/" + this.max.toString();
				}
				else
				{
					tipString = this.current.toString() + "/" + this.max.toString();
				}
				var tipWidth:int = tipString.length * 10;
				tip = new BasicMessage(tipWidth, 25, tipString, this.texRoundRecColor);
				if (leftFlag)
				{
					tip.x = this.barWidth + 5;
				}
				else
				{
					tip.x = - tipWidth - 5;
				}
				tip.y = -5;
				addChild(tip);
			}
		}
		
		public function onMouseOut(event:MouseEvent):void
		{
			if (null !=  tip)
        	{
        		removeChild(tip);
        		tip = null;
        	}
		}
		
		//改变，change为变化的数值
		public function changeBar(change:Number):void
		{
			update(current + change, max);
		}
		
		public function update(current:Number, max:Number):void
		{
			this.current = Math.max(0, Math.min(current, max));
			this.max = max;
			topBarImg.width = buttomBarImg.width * current / max;
		}
		
	}
}